#!/usr/bin/env python
import sys
import os
from fnmatch import fnmatch
from itertools import imap, islice, cycle, dropwhile, ifilter, izip

from walker import Walker

TMP_EMACS = ['*#*#', '*~', '*.#*#']
TMP_PYTHON = ['*.py[co]']
TMP_LOGS = ['*.log']
TMP = TMP_EMACS + TMP_PYTHON + TMP_LOGS


def remove(fname):
    print 'Remove: %s'%fname

def notmatch(x):
    return not fnmatch(*x)

def check(fname, lst=TMP):
    #print fname
    #print lst
    return map(None, islice(dropwhile(notmatch, izip(cycle([fname]), lst)), 1)) and True or False

def rmTmp(pth):
    w = Walker()
    w.file = lambda _pth: map(remove, ifilter(check, [_pth]))
    w.walk(pth)


if __name__=='__main__':
    if len(sys.argv)==2:
        pth = os.path.abspath(sys.argv[1])
        if os.path.exists(pth) and os.path.isdir(pth):
            rmTmp(pth)
    else:
        rmTmp(os.path.abspath('.'))
